package com.ofps.forms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ofps.forms.common.utils.R;
import com.ofps.forms.common.utils.TokenUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ofps.forms.mapper.QuestionSlideMapper;
import com.ofps.forms.domain.QuestionSlide;
import com.ofps.forms.service.QuestionSlideService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

import static com.ofps.common.utils.SecurityUtils.getUserId;


@Service("questionSlideService")
public class QuestionSlideServiceImpl extends ServiceImpl<QuestionSlideMapper, QuestionSlide> implements QuestionSlideService {

    @Resource
    private QuestionSlideMapper questionSlideMapper;

    @Override
    public R delete(Map<String, Object> body) {
        QueryWrapper<QuestionSlide> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", body.get("id"));
        return questionSlideMapper.delete(queryWrapper) == 1 ? R.ok("删除成功！") : R.error("删除失败！");
    }

    @Override
    public R save(HttpServletRequest request, QuestionSlide questionSlide) {
//        questionSlide.setUserId(TokenUtil.getId(request));
        questionSlide.setUserId(Math.toIntExact(getUserId()));
        return this.save(questionSlide) ? R.ok("创建填空题成功！") : R.error("创建失败！");

    }

    @Override
    public List<QuestionSlide> queryQuestionSlideWithId(Map<String, Object> body) {
        QueryWrapper<QuestionSlide> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", body.get("question_id"));
        return questionSlideMapper.selectList(queryWrapper);
    }

}
